<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <!-- 避免IE使用兼容模式 -->
    <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
    <meta name="renderer" content="webkit">
    <!-- TopJUI框架样式 -->
    <link type="text/css" href="/topjui/css/topjui.core.min.css" rel="stylesheet">
    <link type="text/css" href="/topjui/themes/default/topjui.bluelight.css" rel="stylesheet" id="dynamicTheme"/>
    <!-- FontAwesome字体图标 -->
    <link type="text/css" href="/static/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet"/>
    <!-- layui框架样式 -->
    <link type="text/css" href="/static/plugins/layui/css/layui.css" rel="stylesheet"/>
    <link type="text/css" href="/topjui/css/myui.css" rel="stylesheet"/>
    <link rel="stylesheet" href="/static/plugins/kindeditor/themes/default/default.css"/>
    <script type="text/javascript" src="/static/public/js/topjui.config.js"></script>
    <!-- layui框架js -->
    <script type="text/javascript" src="/static/plugins/layui/layui.js" charset="utf-8"></script>
    <script src="/easyui/jquery.min.js"></script>
    <script src="/easyui/jquery.easyui.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="/easyui/locale/easyui-lang-zh_CN.js" type="text/javascript" charset="utf-8"></script>
    <script src="/topjui/js/topjui.core.js"></script>
    <script type="text/javascript" src="/topjui/js/sysutils.js"></script>
    <style>
        fieldset{width:1000px;margin: 10px;border:1px solid #ccc;}
        legend{margin:0 15px;padding: 0 4px }
        .yb_conct{position:fixed;z-index:8010;top:18px;left:1014px;cursor:pointer;transition:all .3s ease;}
        .yb_bar ul li{width:50px;height:50px;font:16px/53px 'Microsoft YaHei';color:#fff;text-indent:54px;margin-bottom:1px;border-radius:3px;transition:all .5s ease;overflow:hidden;}
        .yb_bar .yb_top{background:#00b4ff url(/img/save.png) no-repeat 0 0;background-size:24px 24px;background-position:13px 13px;}
        .yb_bar .yb_clear{background:#00b4ff url(/img/clearup.png) no-repeat 0 0;background-size:24px 24px;background-position:13px 13px;}
        .yb_QQ a{display:block;text-indent:54px;width:100%;height:100%;color:#fff;}
    </style>
</head>
<body>
<form id="customerform" method="post" style="padding-top:1px">
    <input type="hidden" id="id" name="id" th:value="${bean.id}">
    <input type="hidden" id="customerid" name="customerid"  th:value="${bean.customerid}">
    <fieldset>
        <legend>合同基本信息 </legend>
        <table style="margin:6px; width: 98%">
            <tr style="height: 30px">
                <td class="label" width="126px" style="text-align: right">合同编号：</td>
                <td>
                    <input type="text" id="c_code" class="easyui-validatebox selectp" th:value="${bean.code}" name="code" autocomplete="off" data-options="required:true" style="width:270px;height: 24px;" >
                </td>
                <td class="label"  width="126px" style="text-align: right">合同类型：</td>
                <td>
                    <input id="f-type" name="type" type="hidden" th:value="${bean.type}">
                    <input id="form-type" name="type_" class="selectp">
                </td>
            </tr>
            <tr style="height: 30px">
                <td class="label" width="126px" style="text-align: right">合同名称：</td>
                <td>
                    <input type="text" id="c_name" class="easyui-validatebox selectp" th:value="${bean.name}" name="name"  autocomplete="off" data-options="required:true" style="width:270px;height: 24px;">
                </td>
                <td class="label" width="126px" style="text-align: right">销售领域：</td>
                <td>
                    <input id="f-introtype" name="introtype" type="hidden" th:value="${bean.introtype}">
                    <input id="form-introtype" name="introtype_" style="width: 300px;height: 26px;" class="selectp">
                </td>
            </tr>
            <tr style="height: 30px">
                <td class="label" width="126px" style="text-align: right">需方名称：</td>
                <td>
                    <input type="text" id="customername" readonly="readonly" th:value="${bean.customername}" name="customername"  autocomplete="off" class="selectp" required="true" data-options="required:true" style="width:270px;height: 24px;" onfocus="openCustomer()">
                </td>
                <td class="label" width="126px" style="text-align: right">销售渠道：</td>
                <td>
                    <input id="f-channeltype" name="channeltype" type="hidden" th:value="${bean.channeltype}">
                    <input id="form-channeltype" name="channeltype_" style="width: 300px;height: 26px;" class="selectp">
                </td>

            </tr>
            <tr style="height: 30px">
                <td class="label" width="126px" style="text-align: right">需方联系电话：</td>
                <td>
                    <input type="text" id="customerphone" readonly="readonly" th:value="${bean.customerphone}" name="customerphone"  autocomplete="off" class="selectp" required="true" data-options="required:true" style="width:270px;height: 24px;">
                </td>
                <td class="label" width="126px" style="text-align: right">签订人：</td>
                <td>
                    <input type="hidden" class="easyui-validatebox" data-options="required:true" id="form-signmanid" name="signmanid" th:value="${bean.signmanid}" >
                    <input type="hidden" class="easyui-validatebox" data-options="required:true" id="form-signman" name="signman" th:value="${bean.signman}" >
                    <input class="selectp"  type="text" name="sign" id="form-sign">
                </td>
            </tr>
            <tr style="height: 30px">
                <td class="label" width="126px" style="text-align: right">需方联系人：</td>
                <td>
                    <input type="text" id="customerman" readonly="readonly" name="customerman" th:value="${bean.customerman}"  autocomplete="off" class="selectp" required="true" data-options="required:true" style="width:270px;height: 24px;">
                </td>
                <td class="label"  width="126px" style="text-align: right">签订日期：</td>
                <td>
                    <input id="f-crtime" name="crtime" type="hidden" th:value="${bean.crtimeValue}">
                    <input type="text" id="form-crtime" name="crtime_"  th:value="${bean.crtimeValue}" class="inputp" autocomplete="off"  style="width:300px;height: 24px;">
                </td>
            </tr>
            <tr style="height: 30px">
                <td class="label" width="126px" style="text-align: right">签订人电话：</td>
                <td>
                    <input type="text" id="form-signphone" name="signphone" th:value="${bean.signphone}"  autocomplete="off" class="selectp" required="true" data-options="required:true" style="width:270px;height: 24px;">
                </td>
                <td class="label" width="126px" style="text-align: right">到期日期：</td>
                <td>
                    <input id="f-serviceDate" name="serviceDate" type="hidden" th:value="${bean.serviceDateValue}">
                    <input type="text" id="form-serviceDate" name="serviceDate_" th:value="${bean.serviceDateValue}"  class="inputp" autocomplete="off"  style="width:300px;height: 24px;">
                </td>
            </tr>
            <tr style="height: 30px">
                <td class="label" width="126px" style="text-align: right">质保期：</td>
                <td>
                    <select name="serviceTime" style="width: 300px;height: 24px" class="selectp">
                        <option value='一年' th:selected="${bean.serviceTime =='一年'}">一年</option>
                        <option value="二年" th:selected="${bean.serviceTime =='二年'}">二年</option>
                        <option value="三年" th:selected="${bean.serviceTime =='三年'}">三年</option>
                    </select>
                </td>
                <td class="label"  width="126px" style="text-align: right">合同状态：</td>
                <td>
                    <input type="hidden"  id="f-status" name="status" th:value="${bean.status}">
                    <input class="selectp"  type="text" name="status_" id="form-status">
                </td>
            </tr>
            <tr style="height: 30px">
                <td class="label"  width="126px" style="text-align: right">存放地点：</td>
                <td>
                    <input type="text" name="address" autocomplete="off" th:value="${bean.address}" class="easyui-validatebox selectp"  required="true" data-options="required:true" style="width:270px;height: 24px;">
                </td>
                <td class="label" width="126px" style="text-align: right">合同是否上传：</td>
                <td>
                    <select name="file" style="width: 300px;height: 24px" class="selectp">
                        <option value='0' th:selected="${bean.file ==0}">未上传</option>
                        <option value="1" th:selected="${bean.file ==1}">已上传</option>
                    </select>
                </td>
            </tr>
            <tr style="height: 30px">
                <td class="label"  width="126px" style="text-align: right">纸质合同：</td>
                <td>
                    <select name="pageht" style="width: 300px;height: 24px" class="selectp">
                        <option value="1" th:selected="${bean.pageht ==1}">有</option>
                        <option value='0' th:selected="${bean.pageht ==0}">无</option>
                    </select>
                </td>
                <td class="label" width="126px" style="text-align: right">&nbsp;</td>
                <td>
                    &nbsp;
                </td>
            </tr>
        </table>
        <table style="display:none;margin-top:6px; border-top:1px solid #ccc; width: 100%">
            <tr height="100">
                <td height="100">
                    <table id="testitems" class="topjui-datagrid" style="height: 100px;width:100%"></table>
                </td>
            </tr>
        </table>
    </fieldset>
    <fieldset>
        <legend>合同产品信息</legend>
        <table style="margin-top:6px; border-top:1px solid #ccc; width: 100%">
            <tr height="200">
                <td height="200">
                    <table id="productitems" class="topjui-datagrid" style="height: 200px;width:100%"></table>
                </td>
            </tr>
        </table>
    </fieldset>
    <fieldset>
        <legend>合同财务信息</legend>
        <table style="margin:6px; width: 98%">
            <tr style="height: 30px">
                <td class="label"  width="126px" style="text-align: right">付款方式：</td>
                <td>
                    <input type="radio" name="paytype" th:checked="${bean.paytype =='全额付款'}" value="全额付款" style="vertical-align:middle; margin-top:-2px; margin-bottom:1px;" >全额付款 &nbsp;&nbsp;&nbsp;&nbsp;
                    <input type="radio" name="paytype" th:checked="${bean.paytype =='分期付款'}" value="分期付款" style="vertical-align:middle; margin-top:-2px; margin-bottom:1px;">分期付款
                </td>
                <td class="label" width="126px" style="text-align: right">&nbsp;</td>
                <td>
                    &nbsp;
                </td>
            </tr>
        </table>
        <table id="table_payment" th:style="${bean.paytype =='全额付款'?'display:none;margin:6px; width: 98%':'margin:6px; width: 98%'}">
            <tr style="height: 30px">
                <td colspan="4" style="padding-left: 72px">
                    <table>
                        <tr>
                            <td>首付款：</td>
                            <td><input name="paymentFrist" th:value="${bean.paymentFrist}" type="text" class="selectp"  style="width:170px;height: 24px;"></td>
                            <td style="padding-left: 28px">比例：</td>
                            <td><input name="paymentScaleFrist" th:value="${bean.paymentScaleFrist}" type="text" class="selectp"  style="width:90px;height: 24px;"></td>
                            <td style="padding-left: 28px">时间要求：</td>
                            <td><input name="paymentExpFrist" th:value="${bean.paymentExpFrist}" type="text" class="selectp"  style="width:280px;height: 24px;"></td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr style="height: 30px">
                <td colspan="4" style="padding-left: 72px">
                    <table>
                        <tr>
                            <td>到货款：</td>
                            <td><input name="paymentSecond" th:value="${bean.paymentSecond}"  type="text" class="selectp"  style="width:170px;height: 24px;"></td>
                            <td style="padding-left: 28px">比例：</td>
                            <td><input name="paymentScaleSecond" th:value="${bean.paymentScaleSecond}" type="text" class="selectp"  style="width:90px;height: 24px;"></td>
                            <td style="padding-left: 28px">时间要求：</td>
                            <td><input name="paymentExpSecond" th:value="${bean.paymentExpSecond}" type="text" class="selectp"  style="width:280px;height: 24px;"></td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr style="height: 30px">
                <td colspan="4" style="padding-left: 72px">
                    <table>
                        <tr>
                            <td>验收款：</td>
                            <td><input name="paymentThird" th:value="${bean.paymentThird}" type="text" class="selectp"  style="width:170px;height: 24px;"></td>
                            <td style="padding-left: 28px">比例：</td>
                            <td><input name="paymentScaleThird" th:value="${bean.paymentScaleThird}" type="text" class="selectp"  style="width:90px;height: 24px;"></td>
                            <td style="padding-left: 28px">时间要求：</td>
                            <td><input name="paymentExpThird" th:value="${bean.paymentExpThird}" type="text" class="selectp"  style="width:280px;height: 24px;"></td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr style="height: 30px">
                <td colspan="4" style="padding-left: 72px">
                    <table>
                        <tr>
                            <td>质保款：</td>
                            <td><input name="paymentLast" th:value="${bean.paymentLast}" type="text" class="selectp"  style="width:170px;height: 24px;"></td>
                            <td style="padding-left: 28px">比例：</td>
                            <td><input name="paymentScaleLast"  th:value="${bean.paymentScaleLast}"type="text" class="selectp"  style="width:90px;height: 24px;"></td>
                            <td style="padding-left: 28px">时间要求：</td>
                            <td><input name="paymentExpLast" th:value="${bean.paymentExpLast}" type="text" class="selectp"  style="width:280px;height: 24px;"></td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
        <table style="margin:6px; width: 98%">
            <tr style="height: 30px">
                <td class="label"  width="126px" style="text-align: right">合同款：</td>
                <td>
                    <input id="cw_total" name="total" th:value="${bean.total}" type="text" class="selectp"  style="width:270px;height: 24px;">
                </td>
                <td class="label" width="126px" style="text-align: right">应收款：</td>
                <td>
                    <input id="cw_receivables" name="receivables" th:value="${bean.receivables}" type="text" class="selectp"  style="width:270px;height: 24px;">
                </td>
            </tr>
            <tr style="height: 30px">
                <td class="label"  width="126px" style="text-align: right">已收款：</td>
                <td>
                    <input id="cw_payment" name="payment" readonly th:value="${bean.payment}" type="text" class="selectp"  style="width:270px;height: 24px;">
                </td>
                <td class="label" width="126px" style="text-align: right">未收款：</td>
                <td>
                    <input id="cw_lastpay" name="lastpay" th:value="${bean.lastpay}" type="text" class="selectp"  style="width:270px;height: 24px;">
                </td>
            </tr>
            <tr style="height: 30px">
                <td class="label"  width="126px" style="text-align: right">已开票金额：</td>
                <td>
                    <input id="cw_totalbill" name="totalbill" th:value="${bean.totalbill}" type="text" class="selectp"  style="width:270px;height: 24px;">
                </td>
                <td class="label" width="126px" style="text-align: right">最近到款日期：</td>
                <td>
                    <input type="hidden" id="f-lastpay" name="lastpaytime" th:value="${bean.lastpaytimeValue}">
                    <input type="text" id="form-lastpay" name="lastpaytime_"  th:value="${bean.lastpaytimeValue}" class="inputp" autocomplete="off"  style="width:300px;height: 24px;">
                </td>
            </tr>
            <tr style="height: 30px">
                <td class="label"  width="126px" style="text-align: right">违约比例：</td>
                <td>
                    <input name="falsifyScale" th:value="${bean.falsifyScale}" type="text" class="selectp"  style="width:270px;height: 24px;">
                </td>
                <td class="label" width="126px" style="text-align: right">&nbsp;</td>
                <td>
                    &nbsp;
                </td>
            </tr>
        </table>
        <table style="margin-top:6px; border-top:1px solid #ccc; width: 100%">
            <tr height="200">
                <td height="200">
                    <table id="paymentitems" class="topjui-datagrid" style="height: 200px;"></table>
                </td>
            </tr>
        </table>
        <table style="margin-top:6px; border-top:1px solid #ccc; width: 100%">
            <tr height="200">
                <td height="200">
                    <table id="billitems" class="topjui-datagrid" style="height: 200px;"></table>
                </td>
            </tr>
        </table>
    </fieldset>
    <fieldset>
        <legend>合同发货信息</legend>
        <table style="margin:6px; width: 98%">
            <tr style="height: 30px">
                <td class="label" width="126px" style="text-align: right">是否已发货：</td>
                <td>
                    <select name="sendstatus" style="width: 300px;height: 24px" class="selectp">
                        <option value='0' th:selected="${bean.sendstatus ==0}">未发货</option>
                        <option value="1" th:selected="${bean.sendstatus ==1}">已发货</option>
                    </select>
                </td>
                <td class="label"  width="126px" style="text-align: right">发货地点：</td>
                <td>
                    <input name="sendaddress" type="text" class="selectp"  style="width:270px;height: 24px;" th:value="${bean.sendaddress}">
                </td>
            </tr>
        </table>
        <table style="margin-top:6px; border-top:1px solid #ccc; width: 100%">
            <tr height="200">
                <td height="200">
                    <table id="senditems" class="topjui-datagrid" style="height: 200px;"></table>
                </td>
            </tr>
        </table>
    </fieldset>
    <fieldset>
        <legend>分理服务信息</legend>
        <table style="margin:6px; width: 98%">
            <tr style="height: 30px">
                <td class="label"  width="126px" style="text-align: right">是否发卡：</td>
                <td>
                    <input type="radio" name="cardtype" th:checked="${bean.cardtype =='未发卡'}" value="未发卡" style="vertical-align:middle; margin-top:-2px; margin-bottom:1px;" >未发卡 &nbsp;&nbsp;&nbsp;&nbsp;
                    <input type="radio" name="cardtype" th:checked="${bean.cardtype =='已发卡'}" value="已发卡" style="vertical-align:middle; margin-top:-2px; margin-bottom:1px;">已发卡
                </td>
                <td class="label" width="126px" style="text-align: right">&nbsp;</td>
                <td>
                    &nbsp;
                </td>
            </tr>
        </table>
        <table id="table_card" style="margin-top:6px; border-top:1px solid #ccc; width: 100%">
            <tr height="200">
                <td height="200">
                    <table id="carditems" class="topjui-datagrid" style="height: 200px;"></table>
                </td>
            </tr>
        </table>
    </fieldset>

</form>
<script type="text/javascript" charset="utf-8" >
    var customeropen = undefined;
    //检查合同编号是否唯一
    function codeSelect(){
        var id = $("#id").val();
        if(id==0) {
            var code = $('#c_code').val();
            $.ajax({
                url: '/contract/codeSelect',
                method: 'post',
                dataType: 'json',
                data: {"code": code},
                success: function (data) {
                    if (data.statusCode == "200") {
                        $('#c_code').val("");
                        $('#c_name').val("");
                        $.messager.show({
                            title: '提示信息',
                            msg: '合同编号已存在！'
                        });
                    }
                }
            });
            $("#form-status").combobox('select',"合同签订");
        }
    }
    //选择客户信息
    function openCustomer(){
        if(customeropen == undefined) {
            customeropen = true;
            $('<div/>').dialog({
                href: "/contract/customer",
                width: 1006,
                height: 320,
                modal: true,
                title: '客户信息查询',
                onClose: function () {
                    $(this).dialog('destroy');
                },
                onLoad: function () {
                    var d = $(this).closest('.window-body');
                    $('#searchgrid').datagrid({
                        onDblClickRow:function(rowIndex,rowData) {
                            $("#customername").val(rowData.compname);
                            $("#customerphone").val(rowData.telphone);
                            $("#customerman").val(rowData.headname);
                            $("#customerid").val(rowData.id);
                            customeropen = undefined;
                            d.dialog('destroy');
                        }
                    })
                }
            });
        }
    }

    $(function () {
        $('input[type=radio][name=paytype]').change(function() {
            if (this.value == '全额付款') {
                $('#table_payment').css('display','none');
            } else if (this.value == '分期付款') {
                $('#table_payment').css('display','inline');
            }
        });

        $('input[type=radio][name=cardtype]').change(function() {
            if (this.value == '未发卡') {
                $('#table_card').css('display','none');
            } else if (this.value == '已发卡') {
                $('#table_card').css('display','inline');
            }
        });

        $('#form-sign').combotree({
            width: 300,
            height: 25,
            style: "width:300px;height:25px;",
            cls: 'selectp',
            panelWidth: 300,
            hasDownArrow: true,
            url: '/user/xs',
            required: true,
            onSelect: function (data) {
                $("#form-signmanid").val(data.id);
                $("#form-signman").val(data.text);
                $("#form-signphone").val(data.phone);
            },
            onLoadSuccess: function () {
                $('#form-sign').combotree('setValue', '[[${bean.signman}]]');
            }
        });

        $('#form-crtime').datebox({
            formatter: formatter_ymd,
            parser: parser_ymd,
            onSelect: function(date){
                var y = date.getFullYear();
                var m = date.getMonth() + 1;
                var d = date.getDate();
                var result = y + '-' + (m < 10 ? ('0' + m) : m) + '-' + (d < 10 ? ('0' + d) : d);
                $("#f-crtime").val(result);
                return result;
            }
        });
        $('#form-lastpay').datebox({
            formatter: formatter_ymd,
            parser: parser_ymd,
            onSelect: function(date){
                var y = date.getFullYear();
                var m = date.getMonth() + 1;
                var d = date.getDate();
                var result = y + '-' + (m < 10 ? ('0' + m) : m) + '-' + (d < 10 ? ('0' + d) : d);
                $("#f-lastpay").val(result);
                return result;
            }
        });
        $('#form-serviceDate').datebox({
            formatter: formatter_ymd,
            parser: parser_ymd,
            onSelect: function(date){
                var y = date.getFullYear();
                var m = date.getMonth() + 1;
                var d = date.getDate();
                var result = y + '-' + (m < 10 ? ('0' + m) : m) + '-' + (d < 10 ? ('0' + d) : d);
                $("#f-serviceDate").val(result);
                return result;
            }
        });

        $('#form-type').combobox({
            width: 300,
            height: 25,
            style: "width:300px;height:24px;",
            cls: 'selectp',
            panelWidth: 300,
            hasDownArrow: true,
            url: '/sys/groupsText?groups=合同类型',
            required: true,
            onChange: function (n,o) {
                $("#f-type").val(n);
            },
            onLoadSuccess: function () {
                $('#form-type').combobox('setValue', '[[${bean.type}]]');
            }
        });

        $('#form-channeltype').combobox({
            width: 300,
            height: 25,
            style: "width:300px;height:24px;",
            cls: 'selectp',
            panelWidth: 300,
            hasDownArrow: true,
            url: '/sys/groupsText?groups=销售渠道',
            required: true,
            onChange: function (n,o) {
                $("#f-channeltype").val(n);
            },
            onLoadSuccess: function () {
                $('#form-channeltype').combobox('setValue', '[[${bean.channeltype}]]');
            }
        });
        $('#form-introtype').combobox({
            width: 300,
            height: 25,
            style: "width:350px;height:24px;",
            cls: 'selectp',
            panelWidth: 300,
            hasDownArrow: true,
            url: '/sys/groupsText?groups=行业分类',
            required: true,
            onChange: function (n,o) {
                $("#f-introtype").val(n);
            },
            onLoadSuccess: function () {
                $('#form-introtype').combobox('setValue', '[[${bean.introtype}]]');
            }
        });
        $('#form-status').combobox({
            width: 300,
            height: 25,
            style: "width:300px;height:25px;",
            cls: 'selectp',
            panelWidth: 300,
            panelHeight: 'auto', //高度自适应
            hasDownArrow: true,
            url: '/sys/groupsText?groups=合同状态',
            required: true,
            onChange: function (n,o) {
                $("#f-status").val(n);
            },
            onLoadSuccess: function () {
                $('#form-status').combobox('setValue', '[[${bean.status}]]');
            }
        });

        $(".combo-text").removeAttr("style");
        $(".combo-text").attr("style", "width: 300px;height: 25px;;line-height:25px");
        $(".combo-text").addClass("selectp");
        $(".combo-arrow").attr("style", "margin-left: -20px;margin-top: 0px;background-color:rgba(0,0,0,0)");
    });
</script>
<script type="text/javascript" charset="utf-8" th:inline="none">
    var testitems;
    var productitems;
    var senditems;
    var pmentitems;
    var billitems;
    var carditems;
    //没有修改的收款金额
    var oldreceive=undefined;
    //没有修改的开票金额
    var oldbills=undefined;
    var editRow = undefined; //定义全局变量：当前编辑的行
    var armming = [{"id":"军品","text":"军品"},{"id":"民品","text":"民品"}];
    var retype = [{"id":"年卡","text":"年卡"},{"id":"终身","text":"终身"}];
    var space = [{"id":"60秒","text":"60秒"},{"id":"50秒","text":"50秒"},
        {"id":"40秒","text":"40秒"},{"id":"30秒","text":"30秒"},{"id":"20秒","text":"20秒"},
        {"id":"10秒","text":"10秒"},{"id":"5秒","text":"5秒"}];
    var productModeItems = undefined;
    $.ajax({
        url: '/sys/groupsText?groups=产品类型',
        method:'get',
        dataType:'json',
        async:false,//重要，这样才能赋值
        success:function(data){
            productModeItems = eval(data);
        }
    });
    var paytypes = undefined;
    $.ajax({
        url: '/sys/groupsText?groups=到款方式',
        method:'get',
        dataType:'json',
        async:false,//重要，这样才能赋值
        success:function(data){
            paytypes = eval(data);
        }
    });
    var billsls = undefined;
    $.ajax({
        url: '/sys/groupsText?groups=开票税率',
        method:'get',
        dataType:'json',
        async:false,//重要，这样才能赋值
        success:function(data){
            billsls = eval(data);
        }
    });

    $(function(){
        testitems = $('#testitems').datagrid({
            url:"/contract/testItemList",
            method:'get',
            fit : false,
            fitColumns : true,
            border : false,
            idField : 'id',
            sortName : 'id',
            sortOrder : 'desc',
            checkOnSelect : true,
            selectOnCheck : true,
            nowrap : false,
            queryParams:{
                "code":""
            },
            columns:[[
                {
                    field:'id',
                    title:'编号',
                    width:50,
                    checkbox : true
                }]]

        });

        productitems = $('#productitems').datagrid({
            url:"/contract/itemList",
            method:'get',
            fit : false,
            fitColumns : true,
            border : false,
            idField : 'id',
            sortName : 'id',
            sortOrder : 'desc',
            checkOnSelect : true,
            selectOnCheck : true,
            nowrap : false,
            queryParams:{
                "code":""
            },
            columns:[[
                {
                    field:'id',
                    title:'编号',
                    width:50,
                    checkbox : true
                }, {
                    field:'contractCode',
                    title:'合同编号',
                    hidden:true,
                    editor : {
                        type : 'textbox',
                    }
                }, {
                    field:'contractName',
                    title:'合同名称',
                    hidden:true,
                    editor : {
                        type : 'textbox',
                    }
                }, {
                    field:'productname',
                    title:'产品名称',
                    sortable:true,
                    width:150,
                    editor : {
                        type : 'validatebox',
                        options : {
                            required : true
                        }
                    }
                }, {
                    field:'producttype',
                    title:'产品型号',
                    width:150,
                    editor : {
                        type : 'validatebox',
                        options : {
                            required : true
                        }
                    }
                }, {
                    field:'productmode',
                    title:'产品类型',
                    width:60,
                    formatter:function(value){
                        for(var i=0; i<productModeItems.length; i++){
                            if (productModeItems[i].id == value) return productModeItems[i].text;
                        }
                        return value;
                    },
                    editor:{
                        type:'combobox',
                        options:{
                            valueField:'id',
                            textField:'text',
                            editable: false,
                            panelHeight: 'auto', //高度自适应
                            data:productModeItems,
                            required:true,
                        }
                    }
                } ,{
                    field:'armming',
                    title:'是否军品',
                    width:60,
                    formatter:function(value){
                        for(var i=0; i<armming.length; i++){
                            if (armming[i].id == value) return armming[i].text;
                        }
                        return value;
                    },
                    editor:{
                        type:'combobox',
                        options:{
                            valueField:'id',
                            textField:'text',
                            editable: false,
                            panelHeight: 'auto', //高度自适应
                            data:armming,
                            required:true,
                        }
                    }
                }, {
                    field:'productprice',
                    title:'单价',
                    width:60,
                    editor : {
                        type : 'validatebox',
                        options : {
                            required : true,
                            events:{
                                blur: function(){
                                    var productsum = productitems.datagrid('getEditor', {index: editRow, field: 'productsum'});
                                    var sum = $(productsum.target).val();
                                    var producttotal = productitems.datagrid('getEditor', {index: editRow, field: 'producttotal'});
                                    if(sum!=undefined&& sum!=0 && sum!='') {
                                        $(producttotal.target).val(parseFloat($(this).val()) * parseFloat(sum));
                                    }else{
                                        $(productsum.target).val(0);
                                        $(producttotal.target).val(0);
                                    }
                                }
                            },
                        }
                    }
                }, {
                    field:'productsum',
                    title:'数量',
                    width:60,
                    editor : {
                        type : 'validatebox',
                        options : {
                            required : true,
                            events:{
                                blur: function(){
                                    var productprice = productitems.datagrid('getEditor', {index: editRow, field: 'productprice'});
                                    var pp = $(productprice.target).val();
                                    var producttotal = productitems.datagrid('getEditor', {index: editRow, field: 'producttotal'});
                                    $(producttotal.target).val(parseFloat($(this).val())* parseFloat(pp));
                                }
                            },
                        }
                    }

                }, {
                    field:'producttotal',
                    title:'小计',
                    width:60,
                    editor : {
                        type : 'validatebox',
                        options : {
                            required : true,
                        }
                    }
                },{
                    field:'remark',
                    title:'备注',
                    width:160,
                    editor : {
                        type : 'validatebox',
                        options : {
                            required : false,
                        }
                    }
                }
            ]],
            toolbar : [{
                text : '添加',
                iconCls : 'fa fa-plus',
                handler : function() {//添加列表的操作按钮添加，修改，删除等
                    var code = $('#c_code').val();
                    console.log(code);
                    if(code==undefined|| code==''){
                        $.messager.show({
                            title: '提示信息',
                            msg: '合同编号不能为空'
                        });
                        return
                    }
                    //添加时先判断是否有开启编辑的行，如果有则把开户编辑的那行结束编辑
                    if (editRow != undefined) {
                        productitems.datagrid("endEdit",editRow);
                    }
                    //添加时如果没有正在编辑的行，则在datagrid的第一行插入一行
                    if (editRow == undefined) {
                        productitems.datagrid("insertRow",{
                            index : 0, // index start with 0
                            row : {}
                        });
                        //将新插入的那一行开户编辑状态
                        productitems.datagrid("beginEdit",0);
                        //给当前编辑的行赋值
                        editRow = 0;
                    }
                }
            },'-',
                {
                    text : '删除',
                    iconCls : 'fa fa-trash',
                    handler : function() {
                        //删除时先获取选择行
                        var rows = productitems.datagrid("getSelections");
                        //选择要删除的行
                        if (rows.length > 0) {
                            $.messager.confirm("提示","你确定要删除吗?",
                                function(r) {
                                    if (r) {
                                        var ids = [];
                                        for (var i = 0; i < rows.length; i++) {
                                            ids.push(rows[i].id);
                                        }
                                        //将选择到的行存入数组并用,分隔转换成字符串，
                                        //本例只是前台操作没有与数据库进行交互所以此处只是弹出要传入后台的id
                                        //alert(ids.join(','));
                                        $.ajax({
                                            url:"/contract/contractProductRemove",
                                            data:{"ids":ids.join(',')},
                                            method:'post',
                                            dataType:'json',
                                            success:function (data) {
                                                if(data.statusCode=='200') {
                                                    var queryParams = productitems.datagrid('options').queryParams;
                                                    queryParams.code = $('#c_code').val();
                                                    productitems.datagrid("options").queryParams=queryParams;
                                                    productitems.datagrid("reload");

                                                    $.messager.show({
                                                        title: '提示信息',
                                                        msg: data.message
                                                    });
                                                }
                                            },
                                            error:function () {
                                                var queryParams = productitems.datagrid('options').queryParams;
                                                queryParams.code = $('#c_code').val();
                                                productitems.datagrid("options").queryParams=queryParams;
                                                productitems.datagrid("reload");

                                                $.messager.show({
                                                    title: '提示信息',
                                                    msg: "操作失败"
                                                });
                                            }
                                        });
                                    }
                                });
                        } else {
                            $.messager.alert("提示",
                                "请选择要删除的行", "error");
                        }
                    }
                },
                '-',
                {
                    text : '保存',
                    iconCls : 'fa fa-save',
                    handler : function() {
                        //保存时结束当前编辑的行，自动触发onAfterEdit事件如果要与后台交互可将数据通过Ajax提交后台
                        productitems.datagrid("endEdit", editRow);
                    }
                }, '-', {
                    text : '刷新',
                    iconCls : 'fa fa-refresh',
                    handler : function() {
                        //取消当前编辑行把当前编辑行罢undefined回滚改变的数据,取消选择的行
                        editRow = undefined;
                        productitems.datagrid("rejectChanges");
                        productitems.datagrid("unselectAll");
                        var queryParams = productitems.datagrid('options').queryParams;
                        queryParams.code = $('#c_code').val();
                        productitems.datagrid("options").queryParams=queryParams;
                        productitems.datagrid("reload");
                    }
                }],
            onBeforeLoad:function(param){
                param.code = $('#c_code').val();
            },
            onAfterEdit : function(rowIndex, rowData, changes) {
                //endEdit该方法触发此事件
                console.info(rowData);
                var url =  "";
                var msg =  "";
                if(rowData.id==undefined||rowData.id==0){
                    url = "/contract/contractProductAdd";
                    msg =  '新建成功！';
                }else{
                    url = "/contract/contractProductEdit";
                    msg = '修改成功！';
                }

                $.ajax({
                    url:url,
                    data:rowData,
                    method:'post',
                    dataType:'json',
                    success:function (data) {
                        if(data.statusCode=='200') {
                            var queryParams = productitems.datagrid('options').queryParams;
                            queryParams.code = $('#c_code').val();
                            productitems.datagrid("options").queryParams=queryParams;
                            productitems.datagrid("reload");

                            $.messager.show({
                                title: '提示信息',
                                msg: data.message
                            });
                        }
                    },
                    error:function () {
                        var queryParams = productitems.datagrid('options').queryParams;
                        queryParams.code = $('#c_code').val();
                        productitems.datagrid("options").queryParams=queryParams;
                        productitems.datagrid("reload");

                        $.messager.show({
                            title: '提示信息',
                            msg: msg
                        });
                    }
                });

                editRow = undefined;

            },
            onDblClickRow : function(index, row) {
                //双击开启编辑行
                var code = $('#c_code').val();
                if(code==undefined|| code==''){
                    $.messager.show({
                        title: '提示信息',
                        msg: '合同编号不能为空'
                    });
                    return
                }
                if (editRow != undefined) {
                    productitems.datagrid("endEdit", editRow);
                }
                if (editRow == undefined) {
                    productitems.datagrid("beginEdit", index);
                    editRow = index;
                }
            },
            onBeforeEdit:function(index,row) {

            },
            onEndEdit:function (index, row) {
                var armming = $(this).datagrid('getEditor', {index: index, field: 'armming'});
                row.armming = $(armming.target).combobox('getText');
                var productmode = $(this).datagrid('getEditor', {index: index, field: 'productmode'});
                row.productmode = $(productmode.target).combobox('getText');
                var contractCode = $(this).datagrid('getEditor', {index: index, field: 'contractCode'});
                $(contractCode.target).textbox('setValue',$('#c_code').val())
                row.contractCode = $(contractCode.target).textbox('getValue');
                var contractName = $(this).datagrid('getEditor', {index: index, field: 'contractName'});
                $(contractName.target).textbox('setValue',$('#c_name').val())
                row.contractName = $(contractName.target).textbox('getValue');
            }
        });

        senditems = $('#senditems').datagrid({
            url:"/contract/sendItemList",
            method:'get',
            fit : false,
            fitColumns : true,
            border : false,
            idField : 'id',
            sortName : 'id',
            sortOrder : 'desc',
            checkOnSelect : true,
            selectOnCheck : true,
            nowrap : false,
            queryParams:{
                "code":""
            },
            columns:[[
                {
                    field:'id',
                    title:'编号',
                    width:50,
                    checkbox : true
                }, {
                    field:'ctcode',
                    title:'合同编号',
                    hidden:true,
                    editor : {
                        type : 'textbox',
                    }

                }, {
                    field:'ctname',
                    title:'合同名称',
                    hidden:true,
                    editor : {
                        type : 'textbox',
                    }
                },{
                    field:'crtime',
                    title:'发货日期',
                    width:90,
                    editor : {
                        type : 'datebox',
                        options : {
                            required : true,
                            formatter: formatter_ymd,
                            parser: parser_ymd,
                        }
                    }
                }, {
                    field:'productname',
                    title:'发货批次',
                    sortable:true,
                    width:120,
                    editor : {
                        type : 'validatebox',
                        options : {
                            required : true
                        }
                    }

                }, {
                    field:'sendsum',
                    title:'发货金额',
                    width:60,
                    editor : {
                        type : 'validatebox',
                        options : {
                            required : true,
                        }
                    }
                }, {
                    field:'senduser',
                    title:'发货人',
                    width:60,
                    editor : {
                        type : 'validatebox',
                        options : {
                            required : true,
                        }
                    }
                }, {
                    field:'sendaddress',
                    title:'发货地点',
                    width:60,
                    editor : {
                        type : 'validatebox',
                        options : {
                            required : false,
                        }
                    }
                }, {
                    field:'producttype',
                    title:'物流公司',
                    width:60,
                    editor : {
                        type : 'validatebox',
                        options : {
                            required : false,
                        }
                    }
                }, {
                    field:'sendno',
                    title:'发货单号',
                    width:160,
                    editor : {
                        type : 'validatebox',
                        options : {
                            required : false,
                        }
                    }
                }
            ]],
            toolbar : [{
                text : '添加',
                iconCls : 'fa fa-plus',
                handler : function() {//添加列表的操作按钮添加，修改，删除等
                    var code = $('#c_code').val();
                    if(code==undefined|| code==''){
                        $.messager.show({
                            title: '提示信息',
                            msg: '合同编号不能为空'
                        });
                        return
                    }
                    //添加时先判断是否有开启编辑的行，如果有则把开户编辑的那行结束编辑
                    if (editRow != undefined) {
                        senditems.datagrid("endEdit",editRow);
                    }
                    //添加时如果没有正在编辑的行，则在datagrid的第一行插入一行
                    if (editRow == undefined) {
                        senditems.datagrid("insertRow",{
                            index : 0, // index start with 0
                            row : {}
                        });
                        //将新插入的那一行开户编辑状态
                        senditems.datagrid("beginEdit",0);
                        //给当前编辑的行赋值
                        editRow = 0;
                    }
                }
            },'-',
                {
                    text : '删除',
                    iconCls : 'fa fa-trash',
                    handler : function() {
                        //删除时先获取选择行
                        var rows = senditems.datagrid("getSelections");
                        //选择要删除的行
                        if (rows.length > 0) {
                            $.messager.confirm("提示","你确定要删除吗?",
                                function(r) {
                                    if (r) {
                                        var ids = [];
                                        for (var i = 0; i < rows.length; i++) {
                                            ids.push(rows[i].id);
                                        }
                                        //将选择到的行存入数组并用,分隔转换成字符串，
                                        //本例只是前台操作没有与数据库进行交互所以此处只是弹出要传入后台的id
                                        //alert(ids.join(','));
                                        $.ajax({
                                            url:"/contract/sendmkRemove",
                                            data:{"ids":ids.join(',')},
                                            method:'post',
                                            dataType:'json',
                                            success:function (data) {
                                                if(data.statusCode=='200') {
                                                    var queryParams = senditems.datagrid('options').queryParams;
                                                    queryParams.code = $('#c_code').val();
                                                    senditems.datagrid("options").queryParams=queryParams;
                                                    senditems.datagrid("reload");

                                                    $.messager.show({
                                                        title: '提示信息',
                                                        msg: data.message
                                                    });
                                                }
                                            },
                                            error:function () {
                                                var queryParams = senditems.datagrid('options').queryParams;
                                                queryParams.code = $('#c_code').val();
                                                senditems.datagrid("options").queryParams=queryParams;
                                                senditems.datagrid("reload");

                                                $.messager.show({
                                                    title: '提示信息',
                                                    msg: "操作失败"
                                                });
                                            }
                                        });
                                    }
                                });
                        } else {
                            $.messager.alert("提示",
                                "请选择要删除的行", "error");
                        }
                    }
                },
                '-',
                {
                    text : '保存',
                    iconCls : 'fa fa-save',
                    handler : function() {
                        //保存时结束当前编辑的行，自动触发onAfterEdit事件如果要与后台交互可将数据通过Ajax提交后台
                        senditems.datagrid("endEdit", editRow);
                    }
                }, '-', {
                    text : '刷新',
                    iconCls : 'fa fa-refresh',
                    handler : function() {
                        //取消当前编辑行把当前编辑行罢undefined回滚改变的数据,取消选择的行
                        editRow = undefined;
                        senditems.datagrid("rejectChanges");
                        senditems.datagrid("unselectAll");
                        var queryParams = senditems.datagrid('options').queryParams;
                        queryParams.code = $('#c_code').val();
                        senditems.datagrid("options").queryParams=queryParams;
                        senditems.datagrid("reload");
                    }
                }],
            onBeforeLoad:function(param){
                param.code = $('#c_code').val();
            },
            onAfterEdit : function(rowIndex, rowData, changes) {
                //endEdit该方法触发此事件
                console.info(rowData);
                var url =  "";
                var msg =  "";
                if(rowData.id==undefined||rowData.id==0){
                    url = "/contract/sendmkAdd";
                    msg =  '新建成功！';
                }else{
                    url = "/contract/sendmkEdit";
                    msg = '修改成功！';
                }

                $.ajax({
                    url:url,
                    data:rowData,
                    method:'post',
                    dataType:'json',
                    success:function (data) {
                        if(data.statusCode=='200') {
                            var queryParams = senditems.datagrid('options').queryParams;
                            queryParams.code = $('#c_code').val();
                            senditems.datagrid("options").queryParams=queryParams;
                            senditems.datagrid("reload");

                            $.messager.show({
                                title: '提示信息',
                                msg: data.message
                            });
                        }
                    },
                    error:function () {
                        var queryParams = senditems.datagrid('options').queryParams;
                        queryParams.code = $('#c_code').val();
                        senditems.datagrid("options").queryParams=queryParams;
                        senditems.datagrid("reload");

                        $.messager.show({
                            title: '提示信息',
                            msg: msg
                        });
                    }
                });

                editRow = undefined;

            },
            onDblClickRow : function(index, row) {
                //双击开启编辑行
                var code = $('#c_code').val();
                if(code==undefined|| code==''){
                    $.messager.show({
                        title: '提示信息',
                        msg: '合同编号不能为空'
                    });
                    return
                }
                if (editRow != undefined) {
                    senditems.datagrid("endEdit", editRow);
                }
                if (editRow == undefined) {
                    senditems.datagrid("beginEdit", index);
                    editRow = index;
                }
            },
            onBeforeEdit:function(index,row) {

            },
            onEndEdit:function (index, row) {
                var ctcode = $(this).datagrid('getEditor', {index: index, field: 'ctcode'});
                $(ctcode.target).textbox('setValue',$('#c_code').val())
                row.ctcode = $(ctcode.target).textbox('getValue');
                var ctname = $(this).datagrid('getEditor', {index: index, field: 'ctname'});
                $(ctname.target).textbox('setValue',$('#c_name').val())
                row.ctname = $(ctname.target).textbox('getValue');
                var crtime = $(this).datagrid('getEditor', {index: index, field: 'crtime'});
                row.crtime = $(crtime.target)[0].value;
            }
        });

        pmentitems = $('#paymentitems').datagrid({
            url:"/contract/paymentItemList",
            method:'get',
            fit : false,
            fitColumns : true,
            border : false,
            idField : 'id',
            sortName : 'id',
            sortOrder : 'desc',
            checkOnSelect : true,
            selectOnCheck : true,
            nowrap : false,
            queryParams:{
                "code":""
            },
            columns:[[
                {
                    field:'id',
                    title:'编号',
                    width:50,
                    checkbox : true
                }, {
                    field:'ctcode',
                    title:'合同编号',
                    hidden:true,
                    editor : {
                        type : 'textbox',
                    }

                }, {
                    field:'ctname',
                    title:'合同名称',
                    hidden:true,
                    editor : {
                        type : 'textbox',
                    }
                }, {
                    field:'crtime',
                    title:'到款日期',
                    width:120,
                    editor : {
                        type : 'datebox',
                        options : {
                            required : true,
                            formatter: formatter_ymd,
                            parser: parser_ymd,
                        }
                    }

                }, {
                    field:'receive',
                    title:'到款金额',
                    width:80,
                    editor : {
                        type : 'validatebox',
                        options : {
                            required : true
                        }
                    }

                }, {
                    field:'type',
                    title:'到款方式',
                    width:80,
                    formatter:function(value){
                        for(var i=0; i<paytypes.length; i++){
                            if (paytypes[i].id == value) return paytypes[i].text;
                        }
                        return value;
                    },
                    editor:{
                        type:'combobox',
                        options:{
                            valueField:'id',
                            textField:'text',
                            editable: false,
                            panelHeight: 'auto', //高度自适应
                            data:paytypes,
                            required:true,
                        }
                    }
                }, {
                    field:'remark',
                    title:'备注',
                    width:200,
                    editor : {
                        type : 'textbox',
                    }
                }
            ]],
            toolbar : [{
                text : '添加',
                iconCls : 'fa fa-plus',
                handler : function() {//添加列表的操作按钮添加，修改，删除等
                    var code = $('#c_code').val();
                    if(code==undefined|| code==''){
                        $.messager.show({
                            title: '提示信息',
                            msg: '合同编号不能为空'
                        });
                        return
                    }
                    //添加时先判断是否有开启编辑的行，如果有则把开户编辑的那行结束编辑
                    if (editRow != undefined) {
                        pmentitems.datagrid("endEdit",editRow);
                    }
                    //添加时如果没有正在编辑的行，则在datagrid的第一行插入一行
                    if (editRow == undefined) {
                        pmentitems.datagrid("insertRow",{
                            index : 0, // index start with 0
                            row : {}
                        });
                        //将新插入的那一行开户编辑状态
                        pmentitems.datagrid("beginEdit",0);
                        //给当前编辑的行赋值
                        editRow = 0;
                    }
                }
            },'-',
                {
                    text : '删除',
                    iconCls : 'fa fa-trash',
                    handler : function() {
                        //删除时先获取选择行
                        var rows = pmentitems.datagrid("getSelections");
                        //选择要删除的行
                        if (rows.length > 0) {
                            $.messager.confirm("提示","你确定要删除吗?",
                                function(r) {
                                    if (r) {
                                        var ids = [];
                                        for (var i = 0; i < rows.length; i++) {
                                            ids.push(rows[i].id);
                                        }
                                        //将选择到的行存入数组并用,分隔转换成字符串，
                                        //本例只是前台操作没有与数据库进行交互所以此处只是弹出要传入后台的id
                                        //alert(ids.join(','));
                                        $.ajax({
                                            url:"/contract/paymentRemove",
                                            data:{"ids":ids.join(',')},
                                            method:'post',
                                            dataType:'json',
                                            success:function (data) {
                                                if(data.statusCode=='200') {
                                                    var queryParams = pmentitems.datagrid('options').queryParams;
                                                    queryParams.code = $('#c_code').val();
                                                    pmentitems.datagrid("options").queryParams=queryParams;
                                                    pmentitems.datagrid("reload");

                                                    $.messager.show({
                                                        title: '提示信息',
                                                        msg: data.message
                                                    });
                                                }
                                            },
                                            error:function () {
                                                var queryParams = pmentitems.datagrid('options').queryParams;
                                                queryParams.code = $('#c_code').val();
                                                pmentitems.datagrid("options").queryParams=queryParams;
                                                pmentitems.datagrid("reload");

                                                $.messager.show({
                                                    title: '提示信息',
                                                    msg: "操作失败"
                                                });
                                            }
                                        });
                                    }
                                });
                        } else {
                            $.messager.alert("提示",
                                "请选择要删除的行", "error");
                        }
                    }
                },
                '-',
                {
                    text : '保存',
                    iconCls : 'fa fa-save',
                    handler : function() {
                        //保存时结束当前编辑的行，自动触发onAfterEdit事件如果要与后台交互可将数据通过Ajax提交后台
                        pmentitems.datagrid("endEdit", editRow);
                    }
                }, '-', {
                    text : '刷新',
                    iconCls : 'fa fa-refresh',
                    handler : function() {
                        //取消当前编辑行把当前编辑行罢undefined回滚改变的数据,取消选择的行
                        editRow = undefined;
                        pmentitems.datagrid("rejectChanges");
                        pmentitems.datagrid("unselectAll");
                        var queryParams = pmentitems.datagrid('options').queryParams;
                        queryParams.code = $('#c_code').val();
                        pmentitems.datagrid("options").queryParams=queryParams;
                        pmentitems.datagrid("reload");
                    }
                }],
            onBeforeLoad:function(param){
                param.code = $('#c_code').val();
            },
            onAfterEdit : function(rowIndex, rowData, changes) {
                //endEdit该方法触发此事件
                console.info(rowData);
                var url =  "";
                var msg =  "";
                if(rowData.id==undefined||rowData.id==0){
                    url = "/contract/paymentAdd";
                    msg =  '新建成功！';
                }else{
                    url = "/contract/paymentEdit";
                    msg = '修改成功！';
                }

                $.ajax({
                    url:url,
                    data:rowData,
                    method:'post',
                    dataType:'json',
                    success:function (data) {
                        if(data.statusCode=='200') {
                            var queryParams = pmentitems.datagrid('options').queryParams;
                            queryParams.code = $('#c_code').val();
                            pmentitems.datagrid("options").queryParams=queryParams;
                            pmentitems.datagrid("reload");
                            if(rowData.id==undefined||rowData.id==0){
                                //动态修改金额
                                var cwpayment = toDecimal($("#cw_payment").val());
                                var cwreceivables = toDecimal($('#cw_receivables').val());
                                $('#cw_payment').val(toDecimal(cwpayment+toDecimal(rowData.receive)));
                                $("#cw_lastpay").val(toDecimal(cwreceivables-toDecimal($('#cw_payment').val())));
                                $("#f-lastpay").val(rowData.crtime);

                            }else{
                                //动态修改金额
                                var cwpayment = toDecimal($("#cw_payment").val());
                                var cwreceivables = toDecimal($('#cw_receivables').val());
                                $('#cw_payment').val(toDecimal(cwpayment-toDecimal(oldreceive)+toDecimal(rowData.receive)));
                                $("#cw_lastpay").val(toDecimal(cwreceivables-toDecimal($('#cw_payment').val())));
                            }
                            $.messager.show({
                                title: '提示信息',
                                msg: data.message
                            });
                        }
                    },
                    error:function () {
                        var queryParams = pmentitems.datagrid('options').queryParams;
                        queryParams.code = $('#c_code').val();
                        pmentitems.datagrid("options").queryParams=queryParams;
                        pmentitems.datagrid("reload");

                        $.messager.show({
                            title: '提示信息',
                            msg: msg
                        });
                    }
                });

                editRow = undefined;

            },
            onDblClickRow : function(index, row) {
                //双击开启编辑行
                var code = $('#c_code').val();
                if(code==undefined|| code==''){
                    $.messager.show({
                        title: '提示信息',
                        msg: '合同编号不能为空'
                    });
                    return
                }
                if (editRow != undefined) {
                    pmentitems.datagrid("endEdit", editRow);
                }
                if (editRow == undefined) {
                    pmentitems.datagrid("beginEdit", index);
                    editRow = index;
                    oldreceive = row.receive;
                }
            },
            onBeforeEdit:function(index,row) {

            },
            onEndEdit:function (index, row) {
                var type = $(this).datagrid('getEditor', {index: index, field: 'type'});
                row.type = $(type.target).combobox('getText');
                var ctcode = $(this).datagrid('getEditor', {index: index, field: 'ctcode'});
                $(ctcode.target).textbox('setValue',$('#c_code').val())
                row.ctcode = $(ctcode.target).textbox('getValue');
                var ctname = $(this).datagrid('getEditor', {index: index, field: 'ctname'});
                $(ctname.target).textbox('setValue',$('#c_name').val())
                row.ctname = $(ctname.target).textbox('getValue');
                var crtime = $(this).datagrid('getEditor', {index: index, field: 'crtime'});
                row.crtime = $(crtime.target)[0].value;
            }
        });

        billitems = $('#billitems').datagrid({
            url:"/contract/billsItemList",
            method:'get',
            fit : false,
            fitColumns : true,
            border : false,
            idField : 'id',
            sortName : 'id',
            sortOrder : 'desc',
            checkOnSelect : true,
            selectOnCheck : true,
            nowrap : false,
            queryParams:{
                "code":""
            },
            columns:[[
                {
                    field:'id',
                    title:'编号',
                    width:50,
                    checkbox : true
                }, {
                    field:'ctcode',
                    title:'合同编号',
                    hidden:true,
                    editor : {
                        type : 'textbox',
                    }

                }, {
                    field:'ctname',
                    title:'合同名称',
                    hidden:true,
                    editor : {
                        type : 'textbox',
                    }
                }, {
                    field:'crtime',
                    title:'开票日期',
                    sortable:true,
                    width:120,
                    editor : {
                        type : 'datebox',
                        options : {
                            required : true,
                            formatter: formatter_ymd,
                            parser: parser_ymd,
                        }
                    }

                }, {
                    field:'receive',
                    title:'开票金额',
                    width:80,
                    editor : {
                        type : 'validatebox',
                        options : {
                            required : true
                        }
                    }

                }, {
                    field:'suilv',
                    title:'开票税率',
                    width:80,
                    formatter:function(value){
                        for(var i=0; i<billsls.length; i++){
                            if (billsls[i].id == value) return billsls[i].text;
                        }
                        return value;
                    },
                    editor:{
                        type:'combobox',
                        options:{
                            valueField:'id',
                            textField:'text',
                            editable: false,
                            panelHeight: 'auto', //高度自适应
                            data:billsls,
                            required:true,
                        }
                    }
                }, {
                    field:'remark',
                    title:'备注',
                    width:200,
                    editor : {
                        type : 'validatebox',
                        options : {
                            required : false,
                        }
                    }
                }
            ]],
            toolbar : [{
                text : '添加',
                iconCls : 'fa fa-plus',
                handler : function() {//添加列表的操作按钮添加，修改，删除等
                    var code = $('#c_code').val();
                    if(code==undefined|| code==''){
                        $.messager.show({
                            title: '提示信息',
                            msg: '合同编号不能为空'
                        });
                        return
                    }
                    //添加时先判断是否有开启编辑的行，如果有则把开户编辑的那行结束编辑
                    if (editRow != undefined) {
                        billitems.datagrid("endEdit",editRow);
                    }
                    //添加时如果没有正在编辑的行，则在datagrid的第一行插入一行
                    if (editRow == undefined) {
                        billitems.datagrid("insertRow",{
                            index : 0, // index start with 0
                            row : {}
                        });
                        //将新插入的那一行开户编辑状态
                        billitems.datagrid("beginEdit",0);
                        //给当前编辑的行赋值
                        editRow = 0;
                    }
                }
            },'-',
                {
                    text : '删除',
                    iconCls : 'fa fa-trash',
                    handler : function() {
                        //删除时先获取选择行
                        var rows = billitems.datagrid("getSelections");
                        //选择要删除的行
                        if (rows.length > 0) {
                            $.messager.confirm("提示","你确定要删除吗?",
                                function(r) {
                                    if (r) {
                                        var ids = [];
                                        for (var i = 0; i < rows.length; i++) {
                                            ids.push(rows[i].id);
                                        }
                                        //将选择到的行存入数组并用,分隔转换成字符串，
                                        //本例只是前台操作没有与数据库进行交互所以此处只是弹出要传入后台的id
                                        //alert(ids.join(','));
                                        $.ajax({
                                            url:"/contract/billsRemove",
                                            data:{"ids":ids.join(',')},
                                            method:'post',
                                            dataType:'json',
                                            success:function (data) {
                                                if(data.statusCode=='200') {
                                                    var queryParams = billitems.datagrid('options').queryParams;
                                                    queryParams.code = $('#c_code').val();
                                                    billitems.datagrid("options").queryParams=queryParams;
                                                    billitems.datagrid("reload");

                                                    $.messager.show({
                                                        title: '提示信息',
                                                        msg: data.message
                                                    });
                                                }
                                            },
                                            error:function () {
                                                var queryParams = billitems.datagrid('options').queryParams;
                                                queryParams.code = $('#c_code').val();
                                                billitems.datagrid("options").queryParams=queryParams;
                                                billitems.datagrid("reload");

                                                $.messager.show({
                                                    title: '提示信息',
                                                    msg: "操作失败"
                                                });
                                            }
                                        });
                                    }
                                });
                        } else {
                            $.messager.alert("提示",
                                "请选择要删除的行", "error");
                        }
                    }
                },
                '-',
                {
                    text : '保存',
                    iconCls : 'fa fa-save',
                    handler : function() {
                        //保存时结束当前编辑的行，自动触发onAfterEdit事件如果要与后台交互可将数据通过Ajax提交后台
                        billitems.datagrid("endEdit", editRow);
                    }
                }, '-', {
                    text : '刷新',
                    iconCls : 'fa fa-refresh',
                    handler : function() {
                        //取消当前编辑行把当前编辑行罢undefined回滚改变的数据,取消选择的行
                        editRow = undefined;
                        billitems.datagrid("rejectChanges");
                        billitems.datagrid("unselectAll");
                        var queryParams = billitems.datagrid('options').queryParams;
                        queryParams.code = $('#c_code').val();
                        billitems.datagrid("options").queryParams=queryParams;
                        billitems.datagrid("reload");
                    }
                }],
            onBeforeLoad:function(param){
                param.code = $('#c_code').val();
            },
            onAfterEdit : function(rowIndex, rowData, changes) {
                //endEdit该方法触发此事件
                console.info(rowData);
                var url =  "";
                var msg =  "";
                if(rowData.id==undefined||rowData.id==0){
                    url = "/contract/billsAdd";
                    msg =  '新建成功！';
                }else{
                    url = "/contract/billsEdit";
                    msg = '修改成功！';
                }

                $.ajax({
                    url:url,
                    data:rowData,
                    method:'post',
                    dataType:'json',
                    //contentType: 'application/json;charset=UTF-8',
                    success:function (data) {
                        if(data.statusCode=='200') {
                            var queryParams = billitems.datagrid('options').queryParams;
                            queryParams.code = $('#c_code').val();
                            billitems.datagrid("options").queryParams=queryParams;
                            billitems.datagrid("reload");
                            if(rowData.id==undefined||rowData.id==0){
                                //动态修改金额
                                var cwtotalbill = toDecimal($("#cw_totalbill").val());
                                $('#cw_totalbill').val(toDecimal(cwtotalbill+toDecimal(rowData.receive)));

                            }else{
                                //动态修改金额
                                var cwtotalbill = toDecimal($("#cw_totalbill").val());
                                $('#cw_totalbill').val(toDecimal(cwtotalbill-toDecimal(oldbills)+toDecimal(rowData.receive)));
                            }
                            $.messager.show({
                                title: '提示信息',
                                msg: data.message
                            });
                        }
                    },
                    error:function () {
                        var queryParams = billitems.datagrid('options').queryParams;
                        queryParams.code = $('#c_code').val();
                        billitems.datagrid("options").queryParams=queryParams;
                        billitems.datagrid("reload");
                        $.messager.show({
                            title: '提示信息',
                            msg: msg
                        });
                    }
                });

                editRow = undefined;

            },
            onDblClickRow : function(index, row) {
                //双击开启编辑行
                var code = $('#c_code').val();
                if(code==undefined|| code==''){
                    $.messager.show({
                        title: '提示信息',
                        msg: '合同编号不能为空'
                    });
                    return
                }
                if (editRow != undefined) {
                    billitems.datagrid("endEdit", editRow);
                }
                if (editRow == undefined) {
                    billitems.datagrid("beginEdit", index);
                    editRow = index;
                    oldbills = row.receive;
                }
            },
            onBeforeEdit:function(index,row) {

            },
            onEndEdit:function (index, row) {
                var suilv = $(this).datagrid('getEditor', {index: index, field: 'suilv'});
                row.suilv = $(suilv.target).combobox('getText');
                var ctcode = $(this).datagrid('getEditor', {index: index, field: 'ctcode'});
                $(ctcode.target).textbox('setValue',$('#c_code').val())
                row.ctcode = $(ctcode.target).textbox('getValue');
                var ctname = $(this).datagrid('getEditor', {index: index, field: 'ctname'});
                $(ctname.target).textbox('setValue',$('#c_name').val())
                row.ctname = $(ctname.target).textbox('getValue');
                var crtime = $(this).datagrid('getEditor', {index: index, field: 'crtime'});
                row.crtime = $(crtime.target)[0].value;
            }
        });

        carditems = $('#carditems').datagrid({
            url:"/contract/cardItemList",
            method:'get',
            fit : false,
            fitColumns : true,
            border : false,
            idField : 'id',
            sortName : 'id',
            sortOrder : 'desc',
            checkOnSelect : true,
            selectOnCheck : true,
            nowrap : false,
            queryParams:{
                "code":""
            },
            columns:[[
                {
                    field:'id',
                    title:'编号',
                    width:50,
                    checkbox : true
                }, {
                    field:'ctcode',
                    title:'合同编号',
                    hidden:true,
                    editor : {
                        type : 'textbox',
                    }

                }, {
                    field:'ctname',
                    title:'合同名称',
                    hidden:true,
                    editor : {
                        type : 'textbox',
                    }
                },{
                    field:'cardno',
                    title:'用户卡号',
                    width:80,
                    editor : {
                        type : 'validatebox',
                        options : {
                            required : true
                        }
                    }

                }, {
                    field:'zhjno',
                    title:'指挥机卡号',
                    width:80,
                    editor : {
                        type : 'validatebox',
                        options : {
                            required : true
                        }
                    }
                },{
                    field:'space',
                    title:'服务频度',
                    width:80,
                    formatter:function(value){
                        for(var i=0; i<space.length; i++){
                            if (space[i].id == value) return space[i].text;
                        }
                        return value;
                    },
                    editor:{
                        type:'combobox',
                        options:{
                            valueField:'id',
                            textField:'text',
                            editable: false,
                            panelHeight: 'auto', //高度自适应
                            data:space,
                            required:true,
                        }
                    }
                },{
                    field:'total',
                    title:'收费标准',
                    width:80,
                    editor : {
                        type : 'validatebox',
                        options : {
                            required : true
                        }
                    }
                },{
                    field:'retype',
                    title:'收费方式',
                    width:80,
                    formatter:function(value){
                        for(var i=0; i<retype.length; i++){
                            if (retype[i].id == value) return retype[i].text;
                        }
                        return value;
                    },
                    editor:{
                        type:'combobox',
                        options:{
                            valueField:'id',
                            textField:'text',
                            editable: false,
                            panelHeight: 'auto', //高度自适应
                            data:retype,
                            required:true,
                        }
                    }
                },{
                    field:'crtime',
                    title:'起缴日期',
                    sortable:true,
                    width:120,
                    editor : {
                        type : 'datebox',
                        options : {
                            required : true,
                            formatter: formatter_ymd,
                            parser: parser_ymd,
                        }
                    }

                },{
                    field:'sptime',
                    title:'到期日期',
                    sortable:true,
                    width:120,
                    editor : {
                        type : 'datebox',
                        options : {
                            required : true,
                            formatter: formatter_ymd,
                            parser: parser_ymd,
                        }
                    }

                }, {
                    field:'remark',
                    title:'备注',
                    width:80,
                    editor : {
                        type : 'validatebox',
                        options : {
                            required : false
                        }
                    }
                }
            ]],
            toolbar : [{
                text : '添加',
                iconCls : 'fa fa-plus',
                handler : function() {//添加列表的操作按钮添加，修改，删除等
                    var code = $('#c_code').val();
                    if(code==undefined|| code==''){
                        $.messager.show({
                            title: '提示信息',
                            msg: '合同编号不能为空'
                        });
                        return
                    }
                    //添加时先判断是否有开启编辑的行，如果有则把开户编辑的那行结束编辑
                    if (editRow != undefined) {
                        carditems.datagrid("endEdit",editRow);
                    }
                    //添加时如果没有正在编辑的行，则在datagrid的第一行插入一行
                    if (editRow == undefined) {
                        carditems.datagrid("insertRow",{
                            index : 0, // index start with 0
                            row : {}
                        });
                        //将新插入的那一行开户编辑状态
                        carditems.datagrid("beginEdit",0);
                        //给当前编辑的行赋值
                        editRow = 0;
                    }
                }
            },'-',
                {
                    text : '删除',
                    iconCls : 'fa fa-trash',
                    handler : function() {
                        //删除时先获取选择行
                        var rows = carditems.datagrid("getSelections");
                        //选择要删除的行
                        if (rows.length > 0) {
                            $.messager.confirm("提示","你确定要删除吗?",
                                function(r) {
                                    if (r) {
                                        var ids = [];
                                        for (var i = 0; i < rows.length; i++) {
                                            ids.push(rows[i].id);
                                        }
                                        //将选择到的行存入数组并用,分隔转换成字符串，
                                        //本例只是前台操作没有与数据库进行交互所以此处只是弹出要传入后台的id
                                        //alert(ids.join(','));
                                        $.ajax({
                                            url:"/contract/cardRemove",
                                            data:{"ids":ids.join(',')},
                                            method:'post',
                                            dataType:'json',
                                            success:function (data) {
                                                if(data.statusCode=='200') {
                                                    var queryParams = carditems.datagrid('options').queryParams;
                                                    queryParams.code = $('#c_code').val();
                                                    carditems.datagrid("options").queryParams=queryParams;
                                                    carditems.datagrid("reload");

                                                    $.messager.show({
                                                        title: '提示信息',
                                                        msg: data.message
                                                    });
                                                }
                                            },
                                            error:function () {
                                                var queryParams = carditems.datagrid('options').queryParams;
                                                queryParams.code = $('#c_code').val();
                                                carditems.datagrid("options").queryParams=queryParams;
                                                carditems.datagrid("reload");

                                                $.messager.show({
                                                    title: '提示信息',
                                                    msg: "操作失败"
                                                });
                                            }
                                        });
                                    }
                                });
                        } else {
                            $.messager.alert("提示",
                                "请选择要删除的行", "error");
                        }
                    }
                },
                '-',
                {
                    text : '保存',
                    iconCls : 'fa fa-save',
                    handler : function() {
                        //保存时结束当前编辑的行，自动触发onAfterEdit事件如果要与后台交互可将数据通过Ajax提交后台
                        carditems.datagrid("endEdit", editRow);
                    }
                }, '-', {
                    text : '刷新',
                    iconCls : 'fa fa-refresh',
                    handler : function() {
                        //取消当前编辑行把当前编辑行罢undefined回滚改变的数据,取消选择的行
                        editRow = undefined;
                        carditems.datagrid("rejectChanges");
                        carditems.datagrid("unselectAll");
                        var queryParams = carditems.datagrid('options').queryParams;
                        queryParams.code = $('#c_code').val();
                        carditems.datagrid("options").queryParams=queryParams;
                        carditems.datagrid("reload");
                    }
                }],
            onBeforeLoad:function(param){
                param.code = $('#c_code').val();
            },
            onAfterEdit : function(rowIndex, rowData, changes) {
                //endEdit该方法触发此事件
                console.info(rowData);
                var url =  "";
                var msg =  "";
                if(rowData.id==undefined||rowData.id==0){
                    url = "/contract/cardAdd";
                    msg =  '新建成功！';
                }else{
                    url = "/contract/cardEdit";
                    msg = '修改成功！';
                }

                $.ajax({
                    url:url,
                    data:rowData,
                    method:'post',
                    dataType:'json',
                    //contentType: 'application/json;charset=UTF-8',
                    success:function (data) {
                        if(data.statusCode=='200') {
                            var queryParams = carditems.datagrid('options').queryParams;
                            queryParams.code = $('#c_code').val();
                            carditems.datagrid("options").queryParams=queryParams;
                            carditems.datagrid("reload");
                            $.messager.show({
                                title: '提示信息',
                                msg: data.message
                            });
                        }
                    },
                    error:function () {
                        var queryParams = carditems.datagrid('options').queryParams;
                        queryParams.code = $('#c_code').val();
                        carditems.datagrid("options").queryParams=queryParams;
                        carditems.datagrid("reload");
                        $.messager.show({
                            title: '提示信息',
                            msg: msg
                        });
                    }
                });

                editRow = undefined;

            },
            onDblClickRow : function(index, row) {
                //双击开启编辑行
                var code = $('#c_code').val();
                if(code==undefined|| code==''){
                    $.messager.show({
                        title: '提示信息',
                        msg: '合同编号不能为空'
                    });
                    return
                }
                if (editRow != undefined) {
                    carditems.datagrid("endEdit", editRow);
                }
                if (editRow == undefined) {
                    carditems.datagrid("beginEdit", index);
                    editRow = index;
                }
            },
            onBeforeEdit:function(index,row) {

            },
            onEndEdit:function (index, row) {
                var retype = $(this).datagrid('getEditor', {index: index, field: 'retype'});
                row.retype = $(retype.target).combobox('getText');
                var space = $(this).datagrid('getEditor', {index: index, field: 'space'});
                row.space = $(space.target).combobox('getText');
                var ctcode = $(this).datagrid('getEditor', {index: index, field: 'ctcode'});
                $(ctcode.target).textbox('setValue',$('#c_code').val())
                row.ctcode = $(ctcode.target).textbox('getValue');
                var ctname = $(this).datagrid('getEditor', {index: index, field: 'ctname'});
                $(ctname.target).textbox('setValue',$('#c_name').val())
                row.ctname = $(ctname.target).textbox('getValue');
                var crtime = $(this).datagrid('getEditor', {index: index, field: 'crtime'});
                row.crtime = $(crtime.target)[0].value;
                var sptime = $(this).datagrid('getEditor', {index: index, field: 'sptime'});
                row.sptime = $(sptime.target)[0].value;
            }
        });
    });

</script>

<script>
    $(function () {
        // 提交合同
        $(".yb_top").click(function () {
            var id = $("#id").val();
            console.log(id);
            var url = "/contract/save";
            if(id != 0)
                url = "/contract/update";
            console.log(url);
            $('#customerform').form('submit', {
                url : url,
                success : function(result) {
                    try {
                        var r = $.parseJSON(result);
                        if (r.code=='200') {
                            $("#id").val(r.result.id);
                        }
                        $.messager.show({
                            title : "操作提示",
                            msg : r.message
                        });
                    } catch (e) {
                        $.messager.alert('提示', result);
                    }
                }
            });
        });
    });
</script>
<div class="yb_conct">
    <div class="yb_bar">
        <ul>
            <li class="yb_top" title="合同保存">&nbsp;</li>
            <li class="yb_clear" title="重新添加">&nbsp;</li>
        </ul>
    </div>
</div>
</body>
</html>